<template>
  <ex-dialog
    width="1200px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="群名称：" prop="groupName">
              <el-input v-model="ruleForm.groupName" placeholder="请填写群名称" maxlength="50"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="群二维码:" prop="qrcode"  ref="imgRef">
              <ImageUpload :limit="1" @successImg="successImg" :value="ruleForm.qrcode" @input="handleRemove"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="button_box">
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm')"
      >保存提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
import Loading from "@/components/Loading/index";
// api
import {
  addQrcode,
  editQrcode,
  qrcodeDetail,
} from "@/api/setting/wxCodeSetting";
import '@wangeditor/editor/dist/css/style.css'
import {
  Editor,
  Toolbar
} from "@wangeditor/editor-for-vue";
export default {
  components: {
    Loading,
    Editor,
    Toolbar
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "新增群活码",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        groupName: "", //活动名称
        qrcode: "", //活动图片
      },
      residueStock: 0,
      merchantIdList: [],//店铺下拉列表
      merchantEquityCardList: [],//店铺活动资产下拉
      xzAssetList: [],//勋章卡下拉
      advertiseTypeList: [],
      equityCardType: [],
      equityFunctionType: [],
      incentiveTypeList: [
        {id: '1',value: '按交易金额比例激励'},
        {id: '2',value: '按固定金额激励'},
      ],
      integralTypeList: [
        {id: '1',value: 'CoCo'},
        {id: '2',value: '椰分'},
      ],
      rules: {
        groupName: [
          { required: true,message: "请填写群名称",trigger: ["change", "bulr"]}
        ],
      
        qrcode: [
          {required: true, message: "请上传群二维码",trigger: ["change"]}
        ],
      },
    };
  },
  computed: {},
  created() {
    if(this.pathType == 'edit'){
      this.title = '编辑群活码'
      this.ruleForm.id = this.id
      this.getInfo()
    }
  },
  mounted() {
   
  },
  computed: {},
  methods: {
   
    //获取详情
    getInfo(){
      var id = this.ruleForm.id
      qrcodeDetail(id).then(res => {
        this.ruleForm = {
          ...res.result,
          date: []
        }
      })
    },
    
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    successImg(e) {
      this.ruleForm.qrcode = e
      this.$refs['imgRef'].clearValidate();
    },
    // 删除图片
    handleRemove(){
      this.ruleForm.qrcode = ''
    },
    getTime(e){
      console.log("e",e)
      this.ruleForm.startTime =  e[0] 
      this.ruleForm.endTime = e[1]
      console.log("e",this.ruleForm.startTime)
      console.log("e",this.ruleForm.endTime)
    },
    
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate(valid => {
        let isCheck = true;
        let isStock = true;
        let isAmount = true;
        if (valid) {
          this.AgreeLoading = true
          
          var data = {
            ...this.ruleForm
          }
          if(this.pathType == 'add'){
            addQrcode(data).then(res => {
              setTimeout(() => {
                this.handleClose()
                this.$message.success('创建成功')
                this.$emit('success')
                this.AgreeLoading = false
              }, 1000);
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }else if(this.pathType == 'edit'){
            editQrcode(data).then(res => {
              setTimeout(() => {
                this.handleClose()
                this.$message.success('修改成功')
                this.$emit('success')
                this.AgreeLoading = false
              }, 1000);
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}

.main {
  padding: 20px 20px;
}
.button_box{
  display: flex;
  justify-content: center;
}
</style>
